package com.ruan.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.mapper.OnDutyMapper;
import com.ruan.pojo.*;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class OnDutyQuartz extends QuartzJobBean {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private OnDutyMapper onDutyMapper;

    //定时任务
    //通过查询员工信息和考勤记录，来更新员工的考勤状态，在每天的特定时间点执行，
    // 以确保员工的考勤信息得到及时更新和处理
    @Override//executeInternal和参数JobExecutionContext，定时任务常用
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

        //使用了DateTimeFormatter来定义日期格式为"yyyy-MM-dd"，
        // 然后获取当前时间的Date对象，并将其转换为字符串形式。
        DateTimeFormatter dtf= DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String dateStr = date.toString();
        //将日期字符串转换为LocalDate对象，并获取该日期是星期几（value表示一周中的第几天）
        LocalDate localDate = LocalDate.parse(dateStr, dtf);
        //一周中的第几天
        int value = localDate.getDayOfWeek().getValue();

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //查询员工表Employee中状态不是"离职"和待审核的员工列表。
        queryWrapper.ne(Employee::getStatus,"离职");
        queryWrapper.ne(Employee::getStatus,"待审核");
        List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
        //将查询到的符合条件的员工的电话号码存储在phoneList列表中。
        //保持员工的独一无二性
        List<String> phoneList = new ArrayList<>();
        for (Employee employee : employeeList) {
            phoneList.add(employee.getPhone());
        }

        for (String phone : phoneList) {

            LambdaQueryWrapper<Employee> employeeWrapper = new LambdaQueryWrapper<>();
            employeeWrapper.eq(Employee::getPhone,phone);
            Employee employee = employeeMapper.selectOne(employeeWrapper);

            LambdaQueryWrapper<OnDuty> onDutyWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Employee::getPhone,phone);
            Employee employee1 = employeeMapper.selectOne(queryWrapper1);
            onDutyWrapper.eq(OnDuty::getEmployeeId,employee1.getId());
            //查询考勤表OnDuty中是否已存在该员工当天的考勤记录。
            onDutyWrapper.eq(OnDuty::getDay,dateStr);
            OnDuty onDuty = onDutyMapper.selectOne(onDutyWrapper);
            if (onDuty == null){
                //如果不存在考勤记录(没有打卡)，则创建一个新的OnDuty对象，
                OnDuty onDutyAdd = new OnDuty();
                //当天是周六或周日设置员工的考勤状态为"休息"或"旷工"，
                // 然后将该考勤信息插入到数据库中。
                if (value == 6 || value == 7){
                    onDutyAdd.setEmployeeId(employee1.getId());
                    onDutyAdd.setDepartmentId(employee.getDepartmentId());
                    onDutyAdd.setStatus("休息");
                    onDutyAdd.setDay(date);
                    onDutyMapper.insert(onDutyAdd);
                }else {
                    //没有打卡才会标记为旷工
                    onDutyAdd.setEmployeeId(employee1.getId());
                    onDutyAdd.setDepartmentId(employee.getDepartmentId());
                    onDutyAdd.setStatus("旷工");
                    onDutyAdd.setDay(date);
                    onDutyMapper.insert(onDutyAdd);
                }
            }
        }
    }
}
